package xps.and.uudaijia.userclient.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;

import org.greenrobot.eventbus.EventBus;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import xps.and.uudaijia.userclient.data.baen.AliPayResult;

public class APay_v2 {
    private static APay_v2 instance = null;
    private static final int SDK_PAY_FLAG = 1;
    private Context c;

    public static APay_v2 getInstance() {
        if (instance == null) {
            instance = new APay_v2();
        }
        return instance;
    }

    private APay_v2() {
    }

    /**
     * 设置上下文
     *
     * @param context
     */
    public APay_v2 setContext(Context context) {
        this.c = context;
        return this;
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    String payResult = (String) msg.obj;
                    Log.e("handleMessage", payResult);

                    payResult = processResult(payResult);

                    if (payResult.equals("9000")) {
                        Toast.makeText(c, "支付成功", Toast.LENGTH_SHORT).show();
                        EventBus.getDefault().post(new AliPayResult(true));
                    } else {
                        if (payResult.equals("8000")) {
                            Toast.makeText(c, "支付结果确认中", Toast.LENGTH_SHORT).show();
                            EventBus.getDefault().post(new AliPayResult(false));
                        } else {
                            Toast.makeText(c, "支付失败", Toast.LENGTH_SHORT).show();
                            EventBus.getDefault().post(new AliPayResult(false));
                        }
                    }
                    break;
                }
            }

            c = null;
            instance = null;
        }
    };

    public String processResult(String resultString) {
        Pattern pattern = Pattern.compile("[^0-9]");
        Matcher matcher = pattern.matcher(resultString);
        return matcher.replaceAll("");
    }

    public void doPay_v2(String payInfo) {

        Runnable payRunnable = new Runnable() {
            @Override
            public void run() {
                // 构造PayTask 对象
                PayTask alipay = new PayTask((Activity) c);
                // 调用支付接口，获取支付结果
                String result = alipay.pay(payInfo, true);
                if(TextUtils.isEmpty(result))return;
                Message msg = new Message();
                msg.what = SDK_PAY_FLAG;
                msg.obj = result.split("\\;")[0];
                mHandler.sendMessage(msg);
            }
        };

        // 必须异步调用
        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }
}
